from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.throttling import AnonRateThrottle
from rest_framework.viewsets import ModelViewSet
from django_filters.rest_framework import DjangoFilterBackend

from .filters import UserInfoFilter, AddrInfoFilter
from .serializers import UserInfoSerializer, AddrSerializer
from .models import UserInfo, Addr

# 用户信息增删查改


# class UserView(ModelViewSet):
#
#     # 模型类指定
#     queryset = UserInfo.objects.all()
#     # 序列化器指定
#     serializer_class = UserInfoSerializer

"""
    viewsets.ViewSet:
    继承apiview,没有实现任何方法
    
    viewsets.GenericViewSet:
    继承GenericViewSet,没有实现增删查改的动作方法
    只提供了三个方法
        获取单个对象get_queryset()
        获取查询集get_object()
        获取序列化器get_serializer()
    两个类属性
        queryset = UserInfo.objects.all()
        serializer = UserInfoSerializer
        
    viewsets.ReadOnlyModelViewSet:
    
    viewsets.ModelViewSet:
"""


# 收货地址增删查改
class UserView(ModelViewSet):
    # 模型类指定
    queryset = UserInfo.objects.all()
    # 序列化器指定
    serializer_class = UserInfoSerializer
    # 登录的用户可以操作
    # permission_classes = [IsAuthenticatedOrReadOnly]
    # 限流配置
    # throttle_classes = [AnonRateThrottle]
    # 过滤的字段
    # filterset_fields = ['name','id']
    filter_class = UserInfoFilter


class AddrView(ModelViewSet):
    # 模型类指定
    queryset = Addr.objects.all()
    # 序列化器指定
    serializer_class = AddrSerializer
    # permission_class = [IsAuthenticated]
    filter_class = AddrInfoFilter
